var redg = -1080;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listArr: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      sn: options.eq_sn
      // sn: 'f56a0130bb8ee5'
    });
    that.getWaterMakingParam();
  },
  /**
   * 获取制水参数
   */
  getWaterMakingParam() {
    var that = this;
    app.ajax({
      url: 'Manage/Equipment/getWaterMakingParam',
      method: "POST",
      data: {
        sn: that.data.sn
      },
      success: function (res) {
        if (res.data.code == 1000) {
          var data = res.data.data;
          data.forEach(current => {
            current.num = current.value;
            current.animationData = {};
            if (current.type == "time") {
              current.num = that.formatTime(current.num);
            }
          })
          that.setData({
            listArr: data,
            originData: JSON.parse(JSON.stringify(data))
          })
        } else {
          app.showToast("网络错误,请稍后重试", "none", 2000, function () { });
        }
      }
    })
  },

  formatTime(str) {
    if (str.length == 4) {
      return str.substr(0, 2) + ":" + str.substr(2);
    } else {
      return str;
    }
  },


  //增加
  add: function (e) {
    var that = this;
    var indnum = e.currentTarget.dataset.indnum;
    var newArr = that.data.listArr;
    var nowNum = newArr[indnum].num;
    if (nowNum < 9999999) {
      newArr[indnum].num++
    }
    //console.log("修改之后的数组", newArr);
    that.setData({
      listArr: newArr
    })
  },
  //减少
  cut: function (e) {
    var that = this;
    var indnum = e.currentTarget.dataset.indnum;
    var newArr = that.data.listArr;
    var nowNum = newArr[indnum].num;
    if (nowNum > 0) {
      newArr[indnum].num--
    }
    //console.log("修改之后的数组", newArr);
    that.setData({
      listArr: newArr
    })
  },
  //input输入
  inputChnge: function (e) {
    var that = this;
    var inpval = e.detail.value;
    var indnum = e.currentTarget.dataset.indnum;
    var newArr = that.data.listArr;
    if (inpval >= 0) {
      newArr[indnum].num = inpval;
      that.setData({
        listArr: newArr
      })
    }
  },
  //刷新
  updata: function (e) {
    var that = this;
    var indnum = e.currentTarget.dataset.indnum;
    var newArr = that.data.listArr;
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
      success: function (res) {
        console.log("动画结束")
      }
    })
    if (redg) {
      redg -= 1080;
    }
    that.animation = animation
    animation.rotate(redg).step()
    newArr[indnum].animationData = animation //控制对应的动画
    that.setData({
      listArr: newArr
    });
    //请求接口
    app.ajax({
      url: 'Manage/Equipment/waterMakingParamEdit',
      method: "POST",
      data: {
        sn: that.data.sn,
        param: newArr[indnum].id,
        value: newArr[indnum].type == 'time' ? newArr[indnum].num.replace(":", '') : newArr[indnum].num
      },
      success: function (res) {
        if (res.data.code == 1000) {
          const originData = that.data.originData;
          originData[indnum].num = newArr[indnum].num;
          setTimeout(() => {
            that.setData({
              originData
            })
          }, 2000)
          app.showToast(res.data.message, "none", 2000, function () { });
        } else if (res.data.code == -1000) {
          app.showToast(res.data.message, "none", 2000, function () { });
        } else {
          app.showToast("网络错误,请稍后重试", "none", 2000, function () { });
        }
      }
    })
  },

  //关闭时间
  bindtimeChange: function (e) {
    const {
      index
    } = e.currentTarget.dataset;
    const arr = this.data.listArr;
    arr[index].num = e.detail.value;
    this.setData({
      listArr: arr
    })
  }
})